package com.app.biz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.app.biz.constant.BizAppReadConstant;
import com.app.biz.domain.BizAppRead;
import com.app.biz.domain.BizSecurityConsultation;
import com.app.biz.dto.BizAppReadDto;
import com.app.biz.mapper.BizAppReadMapper;
import com.app.biz.mapper.BizSecurityConsultationMapper;
import com.app.biz.service.IBizAppReadService;
import com.app.biz.vo.BizAppReadVo;
import com.app.common.core.domain.model.LoginUser;
import com.app.common.utils.DateUtils;
import com.app.common.utils.SecurityUtils;
import com.app.common.utils.StringUtils;
import com.app.common.utils.uuid.IdUtils;
import com.app.system.domain.SysNotice;
import com.app.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * app消息已读（通知公告及安全咨询）Service业务层处理
 * 
 *
 */
@Service
public class BizAppReadServiceImpl implements IBizAppReadService
{
    @Autowired
    private BizAppReadMapper bizAppReadMapper;

    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private BizSecurityConsultationMapper bizSecurityConsultationMapper;

    /**
     * 查询app消息已读（通知公告及安全咨询）
     * 
     * @param baseId app消息已读（通知公告及安全咨询）主键
     * @return app消息已读（通知公告及安全咨询）
     */
    @Override
    public BizAppRead selectBizAppReadByBaseId(String baseId)
    {
        return bizAppReadMapper.selectBizAppReadByBaseId(baseId);
    }

    /**
     * 查询app消息已读（通知公告及安全咨询）列表
     * 
     * @param bizAppRead app消息已读（通知公告及安全咨询）
     * @return app消息已读（通知公告及安全咨询）
     */
    @Override
    public List<BizAppReadDto> selectBizAppReadList(BizAppReadVo bizAppRead)
    {
        String relateType = bizAppRead.getRelateType();
        if(!StringUtils.isEmpty(relateType) && (BizAppReadConstant.SECURITY_CONSULTATION.equals(relateType) || BizAppReadConstant.NOTICE.equals(relateType))){
            return bizAppReadMapper.selectBizAppReadList(bizAppRead);
        }else{
            return new ArrayList<>();
        }

    }

    /**
     * 新增app消息已读（通知公告及安全咨询）
     * 
     * @param bizAppRead app消息已读（通知公告及安全咨询）
     * @return 结果
     */
    @Override
    public int insertBizAppRead(BizAppRead bizAppRead)
    {
        bizAppRead.setCreateTime(DateUtils.getNowDate());
        return bizAppReadMapper.insertBizAppRead(bizAppRead);
    }

    /**
     * 修改app消息已读（通知公告及安全咨询）
     * 
     * @param bizAppRead app消息已读（通知公告及安全咨询）
     * @return 结果
     */
    @Override
    public int updateBizAppRead(BizAppRead bizAppRead)
    {
        bizAppRead.setUpdateTime(DateUtils.getNowDate());
        return bizAppReadMapper.updateBizAppRead(bizAppRead);
    }

    /**
     * 批量删除app消息已读（通知公告及安全咨询）
     * 
     * @param baseIds 需要删除的app消息已读（通知公告及安全咨询）主键
     * @return 结果
     */
    @Override
    public int deleteBizAppReadByBaseIds(String[] baseIds)
    {
        return bizAppReadMapper.deleteBizAppReadByBaseIds(baseIds);
    }

    /**
     * 删除app消息已读（通知公告及安全咨询）信息
     * 
     * @param baseId app消息已读（通知公告及安全咨询）主键
     * @return 结果
     */
    @Override
    public int deleteBizAppReadByBaseId(String baseId)
    {
        return bizAppReadMapper.deleteBizAppReadByBaseId(baseId);
    }

    @Override
    public int read(BizAppRead bizAppRead) {
        String baseId = bizAppRead.getBaseId();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bizAppRead.setUserId(String.valueOf(loginUser.getUserId()));
        if(StringUtils.isEmpty(baseId)){
            String userId = bizAppRead.getUserId();
            String relateType = bizAppRead.getRelateType();
            String relateId = bizAppRead.getRelateId();
            BizAppRead queryRead = new BizAppRead();
            queryRead.setRelateId(relateId);
            queryRead.setRelateType(relateType);
            queryRead.setUserId(userId);
            List<BizAppRead> existRecordList = bizAppReadMapper.selectList(queryRead);
            if(!CollectionUtils.isEmpty(existRecordList)){
                return 0;
            }else{
                bizAppRead.setBaseId(IdUtils.fastUUID());
                bizAppRead.setStatus(1L);
                bizAppRead.setCreateTime(DateUtils.getNowDate());
                return bizAppReadMapper.insertBizAppRead(bizAppRead);
            }

        }else{
            bizAppRead.setStatus(1L);
            bizAppRead.setUpdateTime(DateUtils.getNowDate());
            return bizAppReadMapper.updateBizAppRead(bizAppRead);
        }

    }

    @Override
    public Object detail(BizAppRead bizAppRead) {
        String relateType = bizAppRead.getRelateType();
        String relateId = bizAppRead.getRelateId();
        if(BizAppReadConstant.NOTICE.equals(relateType)){
            SysNotice sysNotice = noticeMapper.selectNoticeById(Long.valueOf(relateId));
            return sysNotice;
        }else if(BizAppReadConstant.SECURITY_CONSULTATION.equals(relateType)){
            BizSecurityConsultation bizSecurityConsultation = bizSecurityConsultationMapper.selectBizSecurityConsultationByBaseId(relateId);
            return bizSecurityConsultation;

        }else{
            return new Object();
        }

    }

    @Override
    public long selectBizAppUnReadCount(BizAppRead bizAppRead) {
        String relateType = bizAppRead.getRelateType();
        if(!StringUtils.isEmpty(relateType) && (BizAppReadConstant.SECURITY_CONSULTATION.equals(relateType) || BizAppReadConstant.NOTICE.equals(relateType))) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            bizAppRead.setUserId(String.valueOf(loginUser.getUserId()));
            Map map = bizAppReadMapper.selectBizAppUnReadCount(bizAppRead);
            if(map != null){
                return (Long) map.get("unread");
            }else{
                return 0L;
            }
        }else{
            return 0L;
        }

    }
}
